package com.changpei.search.controller;

import com.changpei.common.result.Result;
import com.changpei.search.pojo.FileIndex;
import com.changpei.search.service.FileIndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

/**
 *
 * <p>Title : FileIndex</p>
 * <p>Description : fileIndex的控制层</p>
 * @author weibiao
 * @date 2020年3月7日
 */
@RestController
@RequestMapping("/fileIndex")
@CrossOrigin
public class FileIndexController {

    @Autowired
    private FileIndexService fileIndexService;

    /**
     *
     * <p>Title : add</p>
     * <p>Description : 新增FileIndex数据</p>
     * @param fileIndex
     * @return
     */
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody(required = false) FileIndex fileIndex) {
        // 调用fileIndexService实现添加FileIndex
        return fileIndexService.save(fileIndex);
    }

    /**
     *
     * <p>Title : delete</p>
     * <p>Description : FileIndex根据ID主键删除数据</p>
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public Result<String> delete(@PathVariable(required = false) Long id) {
        // 调用fileIndexService实现根据主键删除
        return fileIndexService.deleteById(id);
    }

    /**
     *
     * <p>Title : update</p>
     * <p>Description : FileIndex根据ID修改</p>
     * @param fileIndex
     * @param id
     * @return
     */
    @PutMapping(value = "/{id}")
    public Result<String> update(@RequestBody FileIndex fileIndex, @PathVariable Long id) {
        // 设置主键值
        fileIndex.setId(id);
        // 调用fileIndexService实现修改FileIndex
        return fileIndexService.update(fileIndex);
    }

    /**
     *
     * <p>Title : findPage</p>
     * <p>Description : FileIndex分页搜索实现</p>
     * @param pageNum : 当前页
     * @param pageSize : 每页显示多少条
     * @return
     */
    @GetMapping(value = "/{pageNum}/{pageSize}")
    public Result<Page<FileIndex>> findPage(@PathVariable int pageNum, @PathVariable int pageSize) {
        // 调用fileIndexService实现分页查询FileIndex
        Page<FileIndex> fileIndices = fileIndexService.search(null, pageNum, pageSize);
        return Result.ok(fileIndices);
    }

    /**
     *
     * <p>Title : findPage</p>
     * <p>Description : FileIndex分页搜索实现</p>
     * @param pageNum : 当前页
     * @param pageSize : 每页显示多少条
     * @return
     */
    @PostMapping(value = "/{pageNum}/{pageSize}")
    public Result<Page<FileIndex>> findPage(@RequestBody(required = false) FileIndex fileIndex, @PathVariable int pageNum, @PathVariable int pageSize) {
        // 调用fileIndexService实现分页查询FileIndex
        Page<FileIndex> pageInfo = fileIndexService.search(fileIndex, pageNum, pageSize);
        return Result.ok(pageInfo);
    }
}
